package cn.htu.javaSong.service.impl;

import cn.htu.javaSong.common.ResponseHelper;
import cn.htu.javaSong.common.ResponseModel;
import cn.htu.javaSong.entity.Song;
import cn.htu.javaSong.mapper.SongMapper;
import cn.htu.javaSong.service.ISongService;
import cn.htu.javaSong.utils.StringUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dakai
 * @since 2024-10-26
 */
@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song> implements ISongService {

    @Value("${songFile}")
    private String songFile;

    @Value("${server.port}")
    private String imgUrl;

    @Override
    public ResponseModel addSong(MultipartFile request) {
        Song song = new Song();
        if (request.isEmpty() || !request.getContentType().startsWith("audio/mpeg")) {
            return ResponseHelper.successMessage("请上传一个有效的MP3文件");
        }
        try {
            // 获取文件名
            String fileName = request.getOriginalFilename();
            // 重命名文件名
            String names = StringUtil.getCurrentDateStr() + ".mp3";
            // 创建文件路径
            Path filePath = Paths.get(songFile + names);
            // 确保目录存在
            File uploadDir = filePath.getParent().toFile();
            song.setUpdateTime(new Date());
            song.setSname(fileName);
            song.setNames(names);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }
            this.save(song);
            // 保存文件
            Files.write(filePath, request.getBytes());
            return ResponseHelper.successMessage("文件上传成功: " + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseHelper.successMessage("文件上传失败");
        }
    }

    @Override
    public ResponseModel addSongs(List<MultipartFile> request) {
        List<String> songName = new ArrayList<>();
        for (MultipartFile res : request) {
            Song song = new Song();
            if (res.isEmpty() || !res.getContentType().startsWith("audio/mpeg")) {
                return ResponseHelper.successMessage("请上传一个有效的MP3文件");
            }
            try {
                // 获取文件名
                String fileName = res.getOriginalFilename();
                songName.add(fileName);
                // 重命名文件名
                String names = StringUtil.getCurrentDateStr() + ".mp3";
                // 创建文件路径
                Path filePath = Paths.get(songFile + names);
                // 确保目录存在
                File uploadDir = filePath.getParent().toFile();
                song.setPath(String.valueOf(filePath));
                song.setUpdateTime(new Date());
                song.setSname(fileName);
                song.setNames(names);
                if (!uploadDir.exists()) {
                    uploadDir.mkdirs();
                }
                try {
                    song.setAddress("http://" + InetAddress.getLocalHost().getHostAddress() + ":" + imgUrl + "/song/" + names);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                this.save(song);
                // 保存文件
                Files.write(filePath, res.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
                return ResponseHelper.successMessage("文件上传失败");
            }
        }
        return ResponseHelper.successMessage("文件上传成功: " + songName);
    }
}
